from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from baozou_selenium import baozou_biz_mysql


def baozou_selenium(Novel_name, insert_time, Chapter, Extension_name, Platform):
    driver = webdriver.Chrome()
    # 进入暴走小说平台

    driver.get("https://cps.baozoukanshu.com/admin/login.html")
    driver.maximize_window()
    driver.implicitly_wait(20)
    # 点击登录渠道
    driver.find_element_by_xpath('//div/div[1]//div[3]/a').click()
    if "羞涩书吧" in Platform:
        # 清理账号
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="account"]'))
        )
        driver.find_element_by_xpath('//*[@id="account"]').clear()
        driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        # 清理密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
        capt = input(">请输入验证码：")
        driver.find_element_by_xpath('//*[@id="captcha"]').send_keys(capt)
        driver.find_element_by_xpath('//*[@id="submit_btn"]').click()
    elif "魔情书苑" in Platform:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="account"]'))
        )
        driver.find_element_by_xpath('//*[@id="account"]').clear()
        driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        # 清理密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
        capt = input(">请输入验证码：")
        driver.find_element_by_name('captcha').send_keys(capt)
        driver.find_element_by_xpath('//*[@id="submit_btn"]').click()
    elif Platform == "暴走（媚色小说）":

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="account"]'))
        )
        driver.find_element_by_xpath('//*[@id="account"]').clear()
        driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        # 清理密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
        capt = input(">请输入验证码：")
        driver.find_element_by_name('captcha').send_keys(capt)
        driver.find_element_by_xpath('//*[@id="submit_btn"]').click()
    elif "色都" in Platform:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="account"]'))
        )
        driver.find_element_by_xpath('//*[@id="account"]').clear()
        driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        # 清理密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
        capt = input(">请输入验证码：")

        driver.find_element_by_name('captcha').send_keys(capt)
        driver.find_element_by_xpath('//*[@id="submit_btn"]').click()
    elif "媚色书吧" in Platform:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="account"]'))
        )
        driver.find_element_by_xpath('//*[@id="account"]').clear()
        driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        # 清理密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
        capt = input(">请输入验证码：")
        driver.find_element_by_name('captcha').send_keys(capt)
        driver.find_element_by_xpath('//*[@id="submit_btn"]').click()
    elif "媚色小说网" in Platform:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="account"]'))
        )
        driver.find_element_by_xpath('//*[@id="account"]').clear()
        driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        # 清理密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
        capt = input(">请输入验证码：")
        driver.find_element_by_name('captcha').send_keys(capt)
        driver.find_element_by_xpath('//*[@id="submit_btn"]').click()
    elif "夜美书院" in Platform:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="account"]'))
        )
        driver.find_element_by_xpath('//*[@id="account"]').clear()
        driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        # 清理密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
        capt = input(">请输入验证码：")
        driver.find_element_by_name('captcha').send_keys(capt)
        driver.find_element_by_xpath('//*[@id="submit_btn"]').click()
    elif "暧昧书海" in Platform:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="account"]'))
        )
        driver.find_element_by_xpath('//*[@id="account"]').clear()
        driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        # 清理密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
        capt = input(">请输入验证码：")
        driver.find_element_by_name('captcha').send_keys(capt)
        driver.find_element_by_xpath('//*[@id="submit_btn"]').click()
    elif "快读爽文" in Platform:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="account"]'))
        )
        driver.find_element_by_xpath('//*[@id="account"]').clear()
        driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        # 清理密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
        capt = input(">请输入验证码：")
        driver.find_element_by_name('captcha').send_keys(capt)
        driver.find_element_by_xpath('//*[@id="submit_btn"]').click()

    else:
        print("没有满足条件的平台")
        # break

    # 弹出框问题
    for i in range(0, 2):
        try:
            time.sleep(0.5)
            WebDriverWait(driver, 5).until(
                EC.element_to_be_clickable((By.XPATH, '//*[@id="layui-layer1"]//a[text()="我知道了"]'))
            )
            driver.find_element_by_xpath('//*[@id="layui-layer1"]/div[3]/a').click()
        except Exception:
            print("无弹出框")
        break

    # 点击链接提取
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//div[2]/div/ul/li[3]/a'))
        )
        time.sleep(0.5)
        driver.find_element_by_xpath('//div[2]/div/ul/li[3]/a').click()
    except Exception:

        print("点击链接提取error")
    # 点击作品链接提取
    try:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//li[3]//dd[2]/a'))
        )
        driver.find_element_by_xpath('//li[3]//dd[2]/a').click()
    except Exception:
        print("点击作品链接提取error")
    # 点击小说查询
    try:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//div[1]/div[2]/div[1]/input'))
        )
        driver.find_element_by_xpath('//div[1]/div[2]/div[1]/input').clear()
        driver.find_element_by_xpath('//div[1]/div[2]/div[1]/input').send_keys('{0}'.format(Novel_name))
        driver.find_element_by_xpath('//div[2]/div[2]/input').click()
    except Exception:
        print("点击小说查询出错")

    try:
        # 点击小说推广链接

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//td[5]/div'))
        )
        driver.find_element_by_xpath('//td[5]/div').click()
    except Exception:
        print("点击小说推广链接error")
    try:
        # 点击章节选择框

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="tselect"]//input'))
        )
        driver.find_element_by_xpath('//*[@id="tselect"]//input').click()
    except Exception:
        print("点击章节选择框error")
    try:
        # 选择推广章节

        driver.find_element_by_xpath('//div/dl/dd[{0}]'.format(Chapter + 1)).click()
    except Exception:
        print("选择推广章节error")

    # 选择渠道名称

    try:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="channelname"]'))
        )
        driver.find_element_by_xpath('//*[@id="channelname"]').clear()
        driver.find_element_by_xpath('//*[@id="channelname"]').send_keys(str(insert_time) + Extension_name)
    except Exception:
        print("选择渠道名称error")

    # 点击生成链接
    try:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//button[1]'))
        )
        driver.find_element_by_xpath('//button[1]').click()
    except Exception:
        print("点击生成链接出错")

    # 选择url框
    try:
        time.sleep(3)
        url = driver.execute_script("return jQuery('[id=urltext]').val();")
        print(url)
        # 数据入库
        try:
            baozou_biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name,
                                                Chapter=Chapter, datetime=insert_time,
                                                Extension_name=str(insert_time) + str(Extension_name))
        except Exception:
            print("数据入库失败")
    except Exception:
        print("选择url框出错")

    # 点击取消按钮
    try:

        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//span[1]/a'))
        )
        driver.find_element_by_xpath('//span[1]/a').click()
    except Exception:
        print("点击取消按钮error")

    driver.quit()
